﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dynamics_AX_System_Administrator_Center
{
    /// <summary>
    /// This class provides integration with Dynamics AX 2012 R2
    /// <remarks> Created by: Sean Galliher Date: 3/4/2013</remarks>
    /// </summary>
    public class DynamicsAXConnector
    {
        WorkListService.EventAlertForUserServiceClient workListSvc;   //reference to the AX Service for interacting with the worklist
        WorkListService.EventAlertForUserServiceReadResponse workListReadResponse; //Read Reponse 
        WorkListService.EventAlertForUserServiceFindResponse workListFindReponse; //Find Reponse
        private List<string> alertslist = new List<string>(); //Alert Worklist Records

        public List<string> AlertList
        {
            get
            {
                return alertslist;
            }
        }
         
        
        /// <summary>
        /// Read a single record from the work list using the AIF web service
        /// </summary>
        public async void readWorkList(string _inboxID)
        {
            //Create new instance of service client
            workListSvc = new WorkListService.EventAlertForUserServiceClient();

            try
            {
                //Read a single work item record
                WorkListService.EntityKey[] readRespKeys = new WorkListService.EntityKey[1];
                readRespKeys[0] = new WorkListService.EntityKey();
                readRespKeys[0].KeyData = new WorkListService.KeyField[3];
                readRespKeys[0].KeyData[0] = new WorkListService.KeyField();
                readRespKeys[0].KeyData[0].Field = "Deleted";
                readRespKeys[0].KeyData[0].Value = "No";
                readRespKeys[0].KeyData[1] = new WorkListService.KeyField();
                readRespKeys[0].KeyData[1].Field = "InboxId";
                readRespKeys[0].KeyData[1].Value = _inboxID;
                
                workListReadResponse = await workListSvc.readAsync(readRespKeys);

                string test = workListReadResponse.EventAlertForUser.EventInbox[0].Subject;

            }

            catch (Exception e)
            {
                string error = e.Message;
                workListSvc.Abort();

            }
        }
       /// <summary>
       /// Find multiple records from the work list using the AIF web service
       /// </summary>
        public async Task<List<string>> findWorkList(string _userID)
        {
            //Create new instance of service client
            workListSvc = new WorkListService.EventAlertForUserServiceClient();

            try
            {

                //Create new query criteria object
                WorkListService.QueryCriteria workListQuery = new WorkListService.QueryCriteria();
                WorkListService.CriteriaElement[] criteria = new WorkListService.CriteriaElement[1];
                criteria[0] = new WorkListService.CriteriaElement();

                //Set query criteria values
                criteria[0].FieldName = "UserID";
                criteria[0].Operator = WorkListService.Operator.Equal;
                criteria[0].Value1 = _userID;
                criteria[0].DataSourceName = "EventInbox";

                //Execute find with criteria
                workListQuery.CriteriaElement = criteria;
                workListFindReponse = await workListSvc.findAsync(workListQuery);

                foreach (var item in workListFindReponse.EventAlertForUser.EventInbox)
                {
                    if (Convert.ToString(item.NotificationSource) == "Alerts")
                    {
                        alertslist.Add("Subject: " + item.Subject + "," + "Type: " + item.NotificationType +","+"Assets/Alert.png" +","+ "Message: " + item.Message);
                    }
                }
                return alertslist;
               
            }
            catch (Exception e)
            {
                string error = e.Message;
                workListSvc.Abort();
                return alertslist;
                
            }

        }

    }

}
